home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 21 / Mac Magazin and MacEasy Magazine CD - Issue 21.iso / Wissenschaft & Technik / yorick12vr1-nofpu folder / include / rezone.i < prev    next >
Text File  |  1995-07-26  |  47KB  |  1,571 lines

  1. /*
  2.    REZONE.I
  3.    Point and click rezoner, written in interpreted Yorick.
  4.  
  5.    $Id$
  6.  */
  7. /*    Copyright (c) 1994.  The Regents of the University of California.
  8.                     All rights reserved.  */
  9.  
  10. require, "button.i";
  11. require, "bowtie.i";
  12.  
  13. func toy_mesh(filename)
  14. /* DOCUMENT toy_mesh, filename
  15.      generates a toy mesh in the file FILENAME in order to be able to
  16.      play with the rezone function.  (FILENAME must be a string enclosed
  17.      in double quotes, of course.)
  18.    SEE ALSO: rezone
  19.  */
  20. {
  21.   rt= span(1., 0., 6)(,-:1:11);
  22.   zt= span(0., 2., 11)(-:1:6,);
  23.   rt(3,3)+= 0.15;
  24.   zt(3,3)+= 0.15;
  25.   rt(3,7)+= 0.05;
  26.   zt(3,7)+= 0.25;
  27.   save, createb(filename), rt, zt;
  28. }
  29.  
  30. extern rez_load_hook;
  31. /* DOCUMENT rez_load_hook
  32.      If non-nil, filename to be included immediately after rez.i.
  33.      This file can be used to set personalized default values for the
  34.      many parameters.
  35.      Some parameters may be set before loading rez.i, as well
  36.    SEE ALSO: rez_kstyle, rez_lstyle
  37.  */
  38.  
  39. extern rez_kstyle, rez_lstyle, rez_color;
  40. /* DOCUMENT rez_kstyle, rez_lstyle, rez_color
  41.      is a vector of three longs:  [type,width,color]
  42.      type MUST be 1, 2, 3, 4, or 5,
  43.      width MUST be 1, 2, 3, 4, 5, 6, 7, or 8,
  44.      and color MUST be -1, -2, -3, -4, -5, -6, -7, -8, -9, or -10
  45.  
  46.      Either or both parameters may be set before or after including
  47.      rez.i; by default:
  48.        rez_kstyle= [4, 1, -2]    (dashdot, thin, foreground color)
  49.        rez_lstyle= [1, 1, -2]    (solid, thin, foreground color)
  50.  
  51.      To distinguish K and L by color instead of type, try:
  52.        rez_kstyle= [1, 1, -7]    (dashdot, thin, blue)
  53.        rez_lstyle= [1, 1, -5]    (solid, thin, red)
  54.      If the variable rez_color is true (non-nil and non-zero) when
  55.      this file is included, then these will be the default.
  56.  
  57.    SEE ALSO: rez_load_hook
  58.  */
  59. /* This choice distinguishes by line type.  */
  60. _old_rez_color= rez_color;
  61. if (is_void(rez_kstyle)) rez_kstyle= rez_color? [1, 1, -7]:[4, 1, -2];
  62. if (is_void(rez_lstyle)) rez_lstyle= rez_color? [1, 1, -5]:[1, 1, -2];
  63.  
  64. extern rez_mark;
  65. /* DOCUMENT rez_mark
  66.      Causes rezone functions to mark bowtied or chevroned (boomeranged)
  67.      zones when the mesh is plotted.  MUST have one of the following values:
  68.        0 to not mark bowtied or chevroned zones
  69.        1 to put a B at the center of the positive triangle of bowtied zones
  70.        2 to put a C at the center of the larger wing of chevroned zones
  71.        3 to mark both bowtied and chevroned zones
  72.  
  73.      By default, rez_mark is 3; you may set it to a different value
  74.      either before or after including rez.i.
  75.  
  76.    SEE ALSO: rez_mesh, rez_next, rez_prev, rez_drag
  77.  */
  78. if (is_void(rez_mark)) rez_mark= 3;
  79.  
  80. _rez_on_black= 0;
  81.  
  82. extern rez_palette;
  83. /* DOCUMENT rez_palette
  84.      3-by-ncolors array used as a palette when rez_regs region coloring
  85.      is set.  The index in rez_regs is the second index into rez_palette.
  86.      The length 3 first index is [r,g,b].
  87.      The default is 15 pastels.
  88.    SEE ALSO: rez_regs
  89.  */
  90. rez_palette= [[255,230,230],[230,255,230],[230,230,255],[242,242,230],
  91.           [230,242,242],[242,230,242],[236,249,230],[236,236,242],
  92.           [249,236,230],[230,236,249],[242,236,236],[230,249,236],
  93.           [249,230,236],[236,242,236],[236,230,249]];
  94. _old_rez_palette= [];
  95.  
  96. func rez_chk_palette
  97. {
  98.   if (typeof(rez_palette)!=typeof(_old_rez_palette) ||
  99.       dimsof(rez_palette)(1)!=dimsof(_old_rez_palette)(1) ||
  100.       anyof(dimsof(rez_palette)!=dimsof(_old_rez_palette)) ||
  101.       anyof(rez_palette!=_old_rez_palette)) {
  102.     if (dimsof(rez_palette)(2)!=3)
  103.       error, "rez_palette must be [[r1,g1,b1],[r2,g2,b2],...]";
  104.     if (!is_array(rez_palette) || typeof(rez_palette)=="struct_instance" ||
  105.     typeof(rez_palette)=="string" || typeof(rez_palette)=="complex" ||
  106.     min(rez_palette)<0 || max(rez_palette)>255)
  107.       error, "rez_palette values should be between 0 and 255";
  108.     palette, rez_palette(1,), rez_palette(2,), rez_palette(3,);
  109.     _old_rez_palette= rez_palette;
  110.     fma;
  111.   }
  112. }
  113.  
  114. extern rez_regs;
  115. /* DOCUMENT rez_regs
  116.      is a vector of region numbers which are to be drawn in the colors
  117.      specified in rez_palette.  Thus:
  118.         rez_regs= [3,5]
  119.      causes regions 3 and 5 to be drawn in colors.  All other regions have
  120.      white backgrounds.  Hilighting too many regions in this way may slow
  121.      down the redraws unacceptably.  To highlight all regions, use:
  122.         rez_regs= indgen(max(rez_ireg))
  123.      This won't work on monochrome monitors, of course.
  124.      The default is rez_regs=[].
  125.    SEE ALSO: rez_palette
  126.  */
  127.  
  128. extern rez_adjust_mesh;
  129. /* DOCUMENT rez_adjust_mesh
  130.      is an optional hook function which is called after any change to the
  131.      mesh (e.g.- by dragging points) has been made.  If it is defined, it
  132.      may modify the external variables rez_rt and rez_zt.  This hook can
  133.      be used, for example, to force points along some k or l line to lie
  134.      along a symmetry line.
  135.    SEE ALSO: rezone, rez_all, rez_mesh
  136.  */
  137.  
  138. func rez_plm(flag, adj_check)
  139. {
  140.   if (adj_check && is_func(rez_adjust_mesh)) rez_adjust_mesh;
  141.  
  142.   fma;
  143.  
  144.   extern rez_kstyle, rez_lstyle;
  145.   if (rez_color!=_old_rez_color) {
  146.     rez_kstyle= rez_color? [1, 1, -7]:[4, 1, -2];
  147.     rez_lstyle= rez_color? [1, 1, -5]:[1, 1, -2];
  148.     _old_rez_color= rez_color;
  149.   }
  150.  
  151.   if (!is_void(rez_regs)) {
  152.     rez_chk_palette;
  153.     n= numberof(rez_regs);
  154.     colors= array(char, dimsof(rez_rt)(2), dimsof(rez_rt)(3));
  155.     for (i=1 ; i<=n ; i++) {
  156.       reg= rez_regs(i);
  157.       if (noneof(rez_ireg==reg) || reg<=0) continue;
  158.       colors(where(rez_ireg==reg))= i-1;
  159.       plf, colors, rez_rt, rez_zt, rez_ireg, legend=string(0), region=reg;
  160.     }
  161.   } else if (_rez_on_black) {
  162.     rez_ptexist;
  163.     list= where(rez_ptx);
  164.     r= rez_rt(list);
  165.     z= rez_zt(list);
  166.     pli, [['\0']], min(z), min(r), max(z), max(r);
  167.   }
  168.  
  169.   plm, rez_rt, rez_zt, rez_ireg, legend=string(0), inhibit=1,
  170.        color= rez_kstyle(3), width= rez_kstyle(2), type= rez_kstyle(1);
  171.   plm, rez_rt, rez_zt, rez_ireg, legend=string(0), inhibit=2,
  172.        color= rez_lstyle(3), width= rez_lstyle(2), type= rez_lstyle(1);
  173.  
  174.   if (rez_mark&3) {
  175.     extern rez_map;
  176.     local rr, zz;
  177.     if (flag || is_void(flag)) {
  178.       if (_rez_polar) {
  179.     zt= rez_zt*cos(rez_rt);
  180.     rt= rez_zt*sin(rez_rt);
  181.       } else {
  182.     zt= rez_zt;
  183.     rt= rez_rt;
  184.       }
  185.       rez_map= bowtie(rt,zt,rez_ireg);
  186.     }
  187.     for (i=1 ; i<=2 ; i++) {
  188.       if (!(rez_mark&i)) continue;
  189.       rez_getrz, where(rez_map==(i-1));
  190.       if (!is_void(rr)) {
  191.     a1= (rr(2,2,)-rr(2,1,))*(zz(2,1,)-zz(1,1,)) -
  192.       (zz(2,2,)-zz(2,1,))*(rr(2,1,)-rr(1,1,));
  193.     a2= (rr(1,1,)-rr(1,2,))*(zz(1,2,)-zz(2,2,)) -
  194.       (zz(1,1,)-zz(1,2,))*(rr(1,2,)-rr(2,2,));
  195.     a3= (rr(2,1,)-rr(1,1,))*(zz(1,1,)-zz(1,2,)) -
  196.       (zz(2,1,)-zz(1,1,))*(rr(1,1,)-rr(1,2,));
  197.     if (i==1) {
  198.       /* this is a magic trick for bowtied zones */
  199.       index= ((a3>=0.) | (a1>=0.)<<1 | (a2>=0.)<<2) - 1;
  200.       p1= [1,0,3,2](index);
  201.       p2= [3,1,2,0](index);
  202.       p3= [2,3,0,1](index);
  203.       p4= [0,2,1,3](index);
  204.       index= indgen(1:numberof(rr):4);
  205.       p1+= index;
  206.       p2+= index;
  207.       p3+= index;
  208.       p4+= index;
  209.       /* now p1 p2 p3 p4 are the indices of the points in (rr,zz)
  210.          with p1 p2 being the positive edge of the bowtied zone,
  211.          so that p1 p3 and p2 p4 cross */
  212.       r1= rr(p1);
  213.       z1= zz(p1);
  214.       r2= rr(p2);
  215.       z2= zz(p2);
  216.       r3= rr(p3);
  217.       z3= zz(p3);
  218.       r4= rr(p4);
  219.       z4= zz(p4);
  220.       fa= (r3-r2)*(z4-z1);
  221.       fb= -(r4-r1)*(z3-z2);
  222.       det= fa+fb;
  223.       fa/= det;
  224.       fb/= det;
  225.       rr= r1*fa + r2*fb + (r3-r2)*(r4-r1)*(z2-z1)/det;
  226.       zz= z2*fa + z1*fb - (z3-z2)*(z4-z1)*(r2-r1)/det;
  227.       rr= (rr+r1+r2)/3.0;
  228.       zz= (zz+z1+z2)/3.0;
  229.       marker= 'B';
  230.       legend= "B: bowtied zones";
  231.     } else {
  232.       a4= (rr(1,2,)-rr(2,2,))*(zz(2,2,)-zz(2,1,)) -
  233.         (zz(1,2,)-zz(2,2,))*(rr(2,2,)-rr(2,1,));
  234.       r1= (rr(1,1,)+rr(2,1,)+rr(2,2,))/3.0;
  235.       z1= (zz(1,1,)+zz(2,1,)+zz(2,2,))/3.0;
  236.       r2= (rr(1,1,)+rr(2,2,)+rr(1,2,))/3.0;
  237.       z2= (zz(1,1,)+zz(2,2,)+zz(1,2,))/3.0;
  238.       r3= (rr(2,1,)+rr(1,1,)+rr(1,2,))/3.0;
  239.       z3= (zz(2,1,)+zz(1,1,)+zz(1,2,))/3.0;
  240.       r4= (rr(1,2,)+rr(2,2,)+rr(2,1,))/3.0;
  241.       z4= (zz(1,2,)+zz(2,2,)+zz(2,1,))/3.0;
  242.       mask= (a1>0. & a2>0.);
  243.       which= [a1*mask,a2*mask,a3*(1-mask),a4*(1-mask)](,mxx);
  244.       n= numberof(a1);
  245.       list= indgen(n)+n*(which-1);
  246.       rr= [r1,r2,r3,r4](list);
  247.       zz= [z1,z2,z3,z4](list);
  248.       marker= 'C';
  249.       legend= "C: chevroned zones";
  250.     }
  251.     plg,rr,zz,type=0,marker=marker,legend=legend;
  252.       }
  253.     }
  254.   }
  255. }
  256.  
  257. _rez_polar= 0n;
  258.  
  259. func rez_toggle
  260. {
  261.   zt= rez_zt;
  262.   rt= rez_rt;
  263.   _rez_polar= !_rez_polar;
  264.   if (_rez_polar) {
  265.     rez_zt= abs(rt,zt);
  266.     rez_rt= atan(rt,zt+1.e-99);
  267.   } else {
  268.     rez_zt= zt*cos(rt);
  269.     rez_rt= zt*sin(rt);
  270.   }
  271. }
  272.  
  273. /* These four parameters specify the viewport position, which is actually
  274.    a function of the stylesheet being used, and potentially changes as
  275.    a function of window or coordinate system within the window.  The numbers
  276.    here should work decently for the work.gs and boxed.gs styles.  If you
  277.    set them very large, the only side effect is that clicking way outside
  278.    the coordinate system (e.g.- missing the Done or Undo button) will pick
  279.    up the nearest mesh vertex).  This is not that unreasonable a behavior,
  280.    especially since any miscue can be undone.  See rez_load_hook.  */
  281. _rez_xvp=  (0.615+0.175)*0.5;
  282. _rez_dxvp= (0.615-0.175)*0.5;
  283. _rez_yvp=  (0.895+0.455)*0.5;
  284. _rez_dyvp= (0.895-0.455)*0.5;
  285. /* Following setup could be placed in rez_load_hook to allow any style:
  286.   _rez_xvp=  0.5;
  287.   _rez_dxvp= 0.6;
  288.   _rez_yvp=  0.5;
  289.   _rez_dyvp= 0.6;
  290.  */
  291.  
  292. func rezone(filename, all=, quiet=)
  293. /* DOCUMENT rezone, filename
  294.        -or- rezone
  295.  
  296.      Invoke the mouse-driven rezoner.  Input is taken from variables
  297.      rt, zt, and ireg in the file FILENAME (which is prompted for if
  298.      omitted).  Output is written to a different file as rt, zt, and
  299.      ireg.  The output file also contains copies rez_rt, rez_zt, and
  300.      rez_ireg in case the mesh under a different name is desirable,
  301.      as well as rez_rt0, rez_zt0, and rez_ireg0, which are the original
  302.      mesh.
  303.  
  304.      If you click on [Escape], you may continue rezoning where you left
  305.      off by typing   rezone   with no arguments.
  306.  
  307.      When you are finished, click [OK] to write the output file.
  308.  
  309.      Use [Zoom or Pan] and [Set Styles] to change the plot limits or
  310.      line colors and styles.  The [Zoom or Pan] screen has buttons which
  311.      "warp" you to each bowtied or chevroned "problem" zone, as well as
  312.      a button to toggle between rectangular and polar coordinates.
  313.  
  314.      Click [Drag Points] to drag points one at a time with the mouse.
  315.  
  316.      Click [Adjust Line] to smooth the points on logical line segments.
  317.      You first click on the endpoints P and Q, then click a button to
  318.      smooth or equal space the poionts between P and Q, or drag any
  319.      point between P and Q to establish equal ratio zones ("ER" operation).
  320.  
  321.      Click [Adjust Region] to smooth the points in logically rectangular
  322.      regions.  You first click on the opposite corners P and R of the
  323.      rectangle PQRS, then click a button to smooth the interior points,
  324.      or click on a point on one of the four edges to transfer the spacing
  325.      on that edge throughout the region PQRS ("EQ" operation).
  326.  
  327.      The [Undo] (or [Redo]) button removes the effect of the most
  328.      recent drag points, adjust line, or adjust region operation.
  329.  
  330.      Hints:
  331.      (0) After [Escape], the current mesh is in the variables
  332.          rez_zt, rez_rt, and rez_ireg.  You can change these
  333.      variables "by hand" before restarting rezone to make
  334.      algebraic adjustments.
  335.      (1) Switch to polar coordinates during an [Adjust Line] or
  336.          [Adjust Region] in order to get points to lie along a
  337.      circular arc.  If your center point is not (0,0), you
  338.      will need to [Escape] and subtract your center point
  339.      from the rez_zt and rez_rt variables yourself, then
  340.      [Escape] a second time to restore the proper coordinates
  341.      before you write the output file.
  342.      (2) You can select a center point as the second point during
  343.          [Adjust Line] or [Adjust Region].  When you do this, the
  344.      K (or L) value of this second point will be the same as
  345.      that of the first point -- otherwise there is no way to
  346.      distinguish among all of the logical points at the center.
  347.      In an [Adjust Region], the center point will be labeled
  348.      Q instead of R as usual, and you will need to choose a
  349.      third point (S) to complete the rectangle PQRS.
  350.      (3) If you set the variable rez_color=1 before you include
  351.          rezone.i, you will get K lines and L lines drawn in different
  352.      colors instead of the default (which is K lines dashdot and
  353.      L lines solid).
  354.      (4) If you have a color monitor, you can set rez_regs before
  355.          invoking rezone in order to highlight a list of regions.
  356.  
  357.      The ALL keyword will be passed to the nbow function, which will
  358.      not be called if the QUIET keyword is non-nil and non-zero.  By
  359.      default, a short summary of bowtied and chevroned zones is printed.
  360.  
  361.      If you need help on how to use one of the screens, try getting
  362.      help on the appropriate on of these SEE ALSO topics, e.g.-
  363.         help, rez_style
  364.      to get help on the [Set Styles] screen.
  365.  
  366.    SEE ALSO: rez_all, rez_drag, rez_ladj, rez_radj, rez_zoom, rez_style,
  367.              rez_color, rez_regs, rez_adjust_mesh, toy_mesh
  368.  */
  369. {
  370.   extern rez_name;
  371.   if (is_void(filename)) {
  372.     if (is_void(rez_name)) {
  373.       filename= "";
  374.       read, format="%s", prompt="Name of Z-file: ", filename;
  375.     }
  376.   }
  377.   if (!is_void(filename)) {
  378.     /* begin a new rezone */
  379.     for (i=1 ; i<=2 ; i++) {
  380.       if (!is_void(open(filename,"rb",1))) break;
  381.       write, "Cannot open "+filename;
  382.       read, format="%s", prompt="Name of Z-file: ", filename;
  383.     }
  384.     if (i>2) {
  385.       write, "Quitting rezone";
  386.       return;
  387.     }
  388.     rez_mesh, openb(filename), quiet=quiet, all=all;
  389.     if (!is_void(rez_zt) && !is_void(rez_rt)) rez_name= filename;
  390.     write, "For help, click [Escape], then type:    help, rezone";
  391.   }
  392.  
  393.   esca= rez_all();
  394.   if (_rez_polar) rez_toggle;
  395.   if (esca) return;
  396.  
  397.   default= rez_name+"_rez";
  398.   for (i=1 ; i<=3 ; i++) {
  399.     line=
  400.       rdline(prompt="Output filename (default "+default+", ? to escape): ");
  401.     line= strtok(line)(1);
  402.     if (!line) line= default;
  403.     else if (line=="?") line= string(0);
  404.     if (line!=rez_name) break;
  405.   }
  406.   if (!line || line==rez_name) {
  407.     write, "Escaping from rezone -- to resume, type    rezone";
  408.     return;
  409.   }
  410.  
  411.   zt= rez_zt;
  412.   rt= rez_rt;
  413.   ireg= rez_ireg;
  414.   at_pdb_close|= 1;
  415.   save, createb(line), zt, rt, ireg, rez_zt, rez_rt, rez_ireg,
  416.     rez_zt0, rez_rt0, rez_ireg0;
  417.  
  418.   rez_name= [];
  419. }
  420.  
  421. func rez_all(dummy)
  422. /* DOCUMENT rez_all
  423.      Click on [Drag Points] to drag points around with the mouse with
  424.      rez_drag.
  425.  
  426.      Click on [Adjust Line] to adjust points along a line segment with
  427.      rez_ladj.
  428.  
  429.      Click on [Adjust Region] to adjust points in a rectangular region
  430.      with rez_radj.
  431.  
  432.      Click on the [Undo] button to undo the previous change.
  433.      Click on the [Redo] button to redo the previous undo.
  434.      Click on the [Revert] to return to the original mesh.
  435.  
  436.      When you are finished moving points around, click on [OK].
  437.      Click on [Escape] to exit with a non-zero return value.
  438.  
  439.      Click on [Zoom or Pan] to change the plot limits (see rez_zoom).
  440.  
  441.      Click on [Set Styles] to change the line styles or colors (see
  442.      rez_style).
  443.  
  444.    SEE ALSO: rez_ladj, rez_mesh, rez_zoom, rez_style, rez_adjust_mesh
  445.  */
  446. {
  447.   done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  448.   undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  449.   esca= Button(text="Escape",x=.344,y=.895,dx=.035,dy=.012);
  450.   zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  451.   sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);
  452.   drag= Button(text="Drag Points",x=.240,y=.375,dx=.054,dy=.012);
  453.   ladj= Button(text="Adjust Line",x=.350,y=.375,dx=.050,dy=.012);
  454.   radj= Button(text="Adjust Region",x=.468,y=.375,dx=.062,dy=.012);
  455.   reve= Button(text="Revert",x=.577,y=.375,dx=.030,dy=.012);
  456.  
  457.   rez_plm, 1;
  458.   button_plot, done, undo, esca, zorp, sets, drag, ladj, radj, reve;
  459.  
  460.   zt= zt2= rez_zt;
  461.   rt= rt2= rez_rt;
  462.  
  463.   for (;;) {
  464.     m= mouse(1, 2, "");
  465.     if (is_void(m)) { rdline,prompt=""; continue; }
  466.     x= m(5);
  467.     y= m(6);
  468.     if (button_test(done,x,y)) break;
  469.     if (button_test(esca,x,y)) {
  470.       write, "Escaping from rezone -- to resume, type    rezone";
  471.       rez_plm,1;
  472.       return 1;
  473.     }
  474.     if (button_test(undo,x,y)) {
  475.       if (undo.text=="Undo") {
  476.     rez_zt= zt;
  477.     rez_rt= rt;
  478.     undo.text= "Redo";
  479.       } else {
  480.     rez_zt= zt2;
  481.     rez_rt= rt2;
  482.     undo.text= "Undo";
  483.       }
  484.       rez_plm, 1;
  485.     } else if (button_test(reve,x,y)) {
  486.       zt= rez_zt;
  487.       rt= rez_rt;
  488.       undo.text= "Undo";
  489.       rez_zt= zt2= rez_zt0;
  490.       rez_rt= rt2= rez_rt0;
  491.       rez_plm, 1;
  492.     } else if (button_test(zorp,x,y)) {
  493.       rez_zoom;
  494.       rez_plm, 0;
  495.     } else if (button_test(sets,x,y)) {
  496.       rez_style;
  497.       rez_plm, 0;
  498.     } else if (button_test(drag,x,y)) {
  499.       zt= rez_zt;
  500.       rt= rez_rt;
  501.       undo.text= "Undo";
  502.       rez_drag;
  503.       zt2= rez_zt;
  504.       rt2= rez_rt;
  505.     } else if (button_test(ladj,x,y)) {
  506.       zt= rez_zt;
  507.       rt= rez_rt;
  508.       undo.text= "Undo";
  509.       rez_ladj;
  510.       zt2= rez_zt;
  511.       rt2= rez_rt;
  512.     } else if (button_test(radj,x,y)) {
  513.       zt= rez_zt;
  514.       rt= rez_rt;
  515.       undo.text= "Undo";
  516.       rez_radj;
  517.       zt2= rez_zt;
  518.       rt2= rez_rt;
  519.     } else {
  520.       rez_ding;
  521.       continue;
  522.     }
  523.     button_plot, done, undo, esca, zorp, sets, drag, ladj, radj, reve;
  524.   }
  525.  
  526.   rez_plm, 1;
  527.   return 0;
  528. }
  529.  
  530. func rez_drag
  531. /* DOCUMENT rez_drag
  532.      Allow points in the rezone mesh to be dragged around with
  533.      the mouse.  Press mouse button near a vertex, move mouse
  534.      to desired position of that vertex, and release button.
  535.      If the point where you click is less than twice the distance to the
  536.      second closest point than to the nearest point, the point will not
  537.      be moved (you will hear a beep when you release the button).
  538.      Click on the [Undo] button to undo the previous move; you can undo
  539.      up to 100 previous moves by successive [Undo] clicks.
  540.  
  541.      When you are finished moving points around, click on [OK].
  542.  
  543.      Click on [Zoom or Pan] to change the plot limits (see rez_zoom).
  544.  
  545.      Click on [Set Styles] to change the line styles or colors (see
  546.      rez_style).
  547.  
  548.    SEE ALSO: rez_ladj, rez_radj, rez_mesh, rez_zoom, rez_style
  549.  */
  550. {
  551.   done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  552.   undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  553.   lab1= Button(text="Drag points.",x=.344,y=.895,dx=.050,dy=.012,width=-1);
  554.   zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  555.   sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);
  556.  
  557.   rez_plm, 1;
  558.   button_plot, done, undo, lab1, zorp, sets;
  559.  
  560.   list= array(0.0, 3, 100);  /* keep last 100 moves for undo */
  561.  
  562.   kmax= dimsof(rez_rt)(2);
  563.   for (;;) {
  564.     m= mouse(1, 2, "");
  565.     if (is_void(m)) { rdline,prompt=""; continue; }
  566.     flag= j= 0;
  567.     if (button_test(done,m(5),m(6))) break;
  568.     if (button_test(undo,m(5),m(6))) {
  569.       j= long(list(1,1));
  570.       zt= list(2,1);
  571.       rt= list(3,1);
  572.       list(,1:-1)= list(,2:0);
  573.       list(,0)= 0.0;
  574.       flag= 1;
  575.     } else if (button_test(zorp,m(5),m(6))) {
  576.       rez_zoom;
  577.       flag= j= 2;
  578.     } else if (button_test(sets,m(5),m(6))) {
  579.       rez_style;
  580.       flag= j= 2;
  581.     } else if (abs(_rez_xvp-m(5))<=_rez_dxvp ||
  582.            abs(_rez_yvp-m(6))<=_rez_dyvp) {
  583.       j= rez_find(m(1), m(2));
  584.       if (numberof(_rez_center)) j= 0;
  585.       zt= m(3);
  586.       rt= m(4);
  587.     }
  588.     if (j<1) {
  589.       rez_ding;
  590.     } else {
  591.       if (flag<2) {
  592.     if (!flag) {
  593.       list(,2:0)= list(,1:-1);
  594.       list(1,1)= j;
  595.       list(2,1)= rez_zt(j);
  596.       list(3,1)= rez_rt(j);
  597.     }
  598.     rez_zt(j)= zt;
  599.     rez_rt(j)= rt;
  600.       }
  601.       rez_plm, (flag<2), 1;
  602.       button_plot, done, undo, lab1, zorp, sets;
  603.       if (!flag) {
  604.     text= swrite(format="moved (%ld,%ld)",(j-1)%kmax+1,(j-1)/kmax+1);
  605.     plt, text, 0.35, 0.375;
  606.       }
  607.     }
  608.   }
  609.  
  610.   rez_plm, 1;
  611. }
  612.  
  613. func rez_ladj
  614. /* DOCUMENT rez_ladj
  615.      Adjust the points along a logical line segment PQ:
  616.  
  617.      Click on [Set P] or [Set Q] to set the endpoints of the segment.
  618.      When P and Q have been set, click [OK]; this button will
  619.      read [Cancel] if P and Q do not have either the same K or the
  620.      same L index.
  621.  
  622.      Click on [Smooth] to smooth the points along PQ.
  623.  
  624.      Click on [Equal Space] to make the points along PQ equally spaced.
  625.      This forces PQ to be a straight line.
  626.  
  627.      Drag any point between P and Q to get equal ratio spacing of all
  628.      the points between P and Q.  This also forces PQ to be a straight
  629.      line; if PQ is initially curved, you may want to click on
  630.      [Equal Space] before you attempt to drag points.
  631.  
  632.      Click on the [Undo] button to undo the previous adjustment; you can
  633.      undo up to 20 previous adjustments by successive [Undo] clicks.
  634.  
  635.      When you are finished adjusting lines, click on [OK].
  636.  
  637.      Click on [Zoom or Pan] to change the plot limits (see rez_zoom).
  638.  
  639.      Click on [Set Styles] to change the line styles or colors (see
  640.      rez_style).
  641.  
  642.      Hint:
  643.          You can select a center point as the second point during
  644.          rez_ladj.  When you do this, the K (or L) value of this second
  645.      point will be the same as that of the first point --
  646.      otherwise there is no way to distinguish among all of the
  647.      logical points at the center.
  648.  
  649.    SEE ALSO: rez_drag, rez_radj, rez_mesh, rez_zoom, rez_style
  650.  */
  651. {
  652.   done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  653.   undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  654.   lab1= Button(text="Adjust line.",x=.344,y=.895,dx=.050,dy=.012,width=-1);
  655.   zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  656.   sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);
  657.  
  658.   setp= Button(text="Set P",x=.208,y=.375,dx=.027,dy=.012);
  659.   setq= Button(text="Set Q",x=.268,y=.375,dx=.027,dy=.012);
  660.   drag= Button(text="Drag for ER",x=.355,y=.375,
  661.            dx=.035,dy=.012,width=-1);
  662.   smth= Button(text="Smooth",x=.454,y=.375,dx=.035,dy=.012);
  663.   eqsp= Button(text="Equal Space",x=.550,y=.375,dx=.054,dy=.012);
  664.  
  665.   mode= 1;
  666.   jP= jQ= oldjP= oldjQ= 0;
  667.   rez_plm, 1;
  668.   kmax= dimsof(rez_rt)(2);
  669.   lab1.text= "Select P.";
  670.   setp.text= "Set Q";
  671.   done.text= "Cancel";
  672.   done.dx= 0.033;
  673.   button_plot, done, lab1, zorp, sets, setp;
  674.  
  675.   for (;;) {
  676.     m= mouse(1, 2, "");
  677.     if (is_void(m)) { rdline,prompt=""; continue; }
  678.     x= m(5);
  679.     y= m(6);
  680.     if (button_test(zorp,x,y)) {
  681.       rez_zoom;
  682.     } else if (button_test(sets,x,y)) {
  683.       rez_style;
  684.     } else if (button_test(setp,x,y)) {
  685.       mode= 1 + (setp.text!="Set P");
  686.       if (mode==1) {
  687.     lab1.text= "Select P.";
  688.     setp.text= "Set Q";
  689.       } else {
  690.     lab1.text= "Select Q.";
  691.     setp.text= "Set P";
  692.       }
  693.  
  694.     } else if (mode) {
  695.       if (button_test(done,x,y)) {
  696.     if (done.text=="Cancel") {
  697.       if (!oldjP || !oldjQ) break;
  698.       jP= oldjP;
  699.       jQ= oldjQ;
  700.     } else {
  701.       if (kP==kQ) {
  702.         k= kP;
  703.         l= rez_range(min(lP,lQ):max(lP,lQ));
  704.       } else {
  705.         l= lP;
  706.         k= rez_range(min(kP,kQ):max(kP,kQ));
  707.       }
  708.       zt= rez_zt(k,l);
  709.       rt= rez_rt(k,l);
  710.       list= array([zt,rt], 20);  /* keep last 20 for undo */
  711.     }
  712.     lab1.text= "Adjust line.";
  713.     setp.text= "Set P";
  714.     mode= 0;
  715.       } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
  716.            abs(_rez_yvp-y)<=_rez_dyvp) {
  717.     j= rez_find(m(1), m(2));
  718.     if (numberof(_rez_center)>1) {
  719.       kc= (_rez_center-1)%kmax + 1;
  720.       lc= (_rez_center-1)/kmax + 1;
  721.       if (noneof(kc-kc(1))) kc= kc(1);
  722.       else if (noneof(lc-lc(1))) lc= lc(1);
  723.       if (mode==1 && jQ>0) {
  724.         kd= (jQ-1)%kmax + 1;
  725.         ld= (jQ-1)/kmax + 1;
  726.         if (numberof(kc)==1 && kc!=kd && anyof(lc==ld))
  727.           j= kc + (ld-1)*kmax;
  728.         else if (numberof(lc)==1 && lc!=ld && anyof(kc==kd))
  729.           j= kd + (lc-1)*kmax;
  730.       } else if (mode!=1 && jP>0) {
  731.         kd= (jP-1)%kmax + 1;
  732.         ld= (jP-1)/kmax + 1;
  733.         if (numberof(kc)==1 && kc!=kd && anyof(lc==ld))
  734.           j= kc + (ld-1)*kmax;
  735.         else if (numberof(lc)==1 && lc!=ld && anyof(kc==kd))
  736.           j= kd + (lc-1)*kmax;
  737.       }
  738.     }
  739.     if (j<1) {
  740.       rez_ding;
  741.       continue;
  742.     } else {
  743.       if (mode==1) jP= j;
  744.       else jQ= j;
  745.       kP= (jP-1)%kmax + 1;
  746.       lP= (jP-1)/kmax + 1;
  747.       kQ= (jQ-1)%kmax + 1;
  748.       lQ= (jQ-1)/kmax + 1;
  749.     }
  750.       } else {
  751.     rez_ding;
  752.     continue;
  753.       }
  754.  
  755.     } else {
  756.       if (button_test(done,x,y)) break;
  757.       if (button_test(undo,x,y)) {
  758.     rez_zt(k,l)= list(,1,1);
  759.     rez_rt(k,l)= list(,2,1);
  760.     list(,,1:-1)= list(,,2:0);
  761.       } else if (button_test(setq,x,y)) {
  762.     oldjP= jP;
  763.     oldjQ= jQ;
  764.     lab1.text= "Select Q.";
  765.     mode= 2;
  766.       } else if (button_test(smth,x,y)) {
  767.     zt= rez_zt(k,l);
  768.     if (numberof(zt)>2) {
  769.       rt= rez_rt(k,l);
  770.       list(,,2:0)= list(,,1:-1);
  771.       list(,1,1)= zt;
  772.       list(,2,1)= rt;
  773.       zt(2:-1)= zt(zcen)(zcen);
  774.       rt(2:-1)= rt(zcen)(zcen);
  775.       rez_zt(k,l)= zt;
  776.       rez_rt(k,l)= rt;
  777.     }
  778.       } else if (button_test(eqsp,x,y)) {
  779.     zt= rez_zt(k,l);
  780.     n= numberof(zt);
  781.     if (n>2) {
  782.       rt= rez_rt(k,l);
  783.       list(,,2:0)= list(,,1:-1);
  784.       list(,1,1)= zt;
  785.       list(,2,1)= rt;
  786.       rez_zt(k,l)= span(zt(1), zt(0), n);
  787.       rez_rt(k,l)= span(rt(1), rt(0), n);
  788.     }
  789.       } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
  790.            abs(_rez_yvp-y)<=_rez_dyvp) {
  791.     j= rez_find(m(1), m(2));
  792.     if (j<=min(jP,jQ) || j>=max(jP,jQ) ||
  793.         (kP==kQ && abs(j-jP)%kmax)) {
  794.       rez_ding;
  795.       continue;
  796.     } else {
  797.       zP= rez_zt(jP);
  798.       rP= rez_rt(jP);
  799.       zQ= rez_zt(jQ);
  800.       rQ= rez_rt(jQ);
  801.       zx= m(3)-zP;
  802.       rx= m(4)-rP;
  803.       za= zQ-zP;
  804.       ra= rQ-rP;
  805.       /* set sx to projection of new point onto PQ, normalized to
  806.          lie between 0 (at P) and 1 (at Q) */
  807.       sx= (za*zx+ra*rx)/abs(za,ra,1.e-35)^2;
  808.       if (sx>0. && sx<1.) {
  809.         zt= rez_zt(k,l);
  810.         rt= rez_rt(k,l);
  811.         list(,,2:0)= list(,,1:-1);
  812.         list(,1,1)= zt;
  813.         list(,2,1)= rt;
  814.         n= numberof(zt)-1;  /* already guaranteed >1 */
  815.         if (kP==kQ) m= abs((j-1)/kmax + 1 - lP);
  816.         else m= abs((j-1)%kmax + 1 - kP);
  817.         m= 1./m;
  818.         r= rez_solve(1./sx, n*m)^m;
  819.         sa= spanl(1., r^(n-1), n)(cum);
  820.         sx= [0.0, sa(0)];
  821.         zt= interp([zP,zQ], sx, sa);
  822.         rt= interp([rP,rQ], sx, sa);
  823.         if (kP>kQ || lP>lQ) {
  824.           zt= zt(0:1:-1);
  825.           rt= rt(0:1:-1);
  826.         }
  827.         rez_zt(k,l)= zt;
  828.         rez_rt(k,l)= rt;
  829.       } else {
  830.         rez_ding;
  831.         continue;
  832.       }
  833.     }
  834.       } else {
  835.     rez_ding;
  836.     continue;
  837.       }
  838.     }
  839.     rez_plm, !mode, 1;
  840.     if (!mode || (jP && jQ && ((kQ==kP) ~ (lQ==lP)))) {
  841.       done.text= "OK";
  842.       done.dx= 0.020;
  843.     } else {
  844.       done.text= "Cancel";
  845.       done.dx= 0.033;
  846.     }
  847.     button_plot, done, lab1, zorp, sets, setp;
  848.     if (jP) plg, rez_rt(kP,lP), rez_zt(kP,lP),
  849.       marker='P', type=0, legend=string(0);
  850.     if (jQ) plg, rez_rt(kQ,lQ), rez_zt(kQ,lQ),
  851.       marker='Q', type=0, legend=string(0);
  852.     if (!mode) button_plot, undo, setq, drag, smth, eqsp;
  853.     else if (jP || jQ) {
  854.       text= jP? swrite(format="P=(%ld,%ld)",kP,lP) : "";
  855.       if (jQ) text+= swrite(format="    Q=(%ld,%ld)",kQ,lQ);
  856.       plt, text, 0.35, 0.375;
  857.     }
  858.   }
  859.  
  860.   rez_plm, 1;
  861. }
  862.  
  863. func rez_radj
  864. /* DOCUMENT rez_radj
  865.      Adjust the points in a logical rectangle PQRS:
  866.  
  867.      Click on [Set P] or [Set R] to set the endpoints of the segment.
  868.      When P and R have been set, click [OK]; this button will
  869.      read [Cancel] if P and R have the same K or the same L index.
  870.  
  871.      Click on a point on any edge of PQRS (but not one of the corners)
  872.      to copy the spacings on that edge throughout the entire region.
  873.      All of the lines of the same type (K-line or L-line) as the edge
  874.      you clicked on will become straight lines.
  875.  
  876.      Click on [Smooth] to smooth the points interior to PQRS.
  877.      Click on [Smooth 4] to perform the smooth operation four times.
  878.  
  879.      Click on the [Undo] button to undo the previous adjustment; you can
  880.      undo up to 10 previous adjustments by successive [Undo] clicks.
  881.  
  882.      When you are finished adjusting regions, click on [OK].
  883.  
  884.      Click on [Zoom or Pan] to change the plot limits (see rez_zoom).
  885.  
  886.      Click on [Set Styles] to change the line styles or colors (see
  887.      rez_style).
  888.  
  889.      Hint:
  890.          You can select a center point as the second point during
  891.          rez_radj.  When you do this, the K (or L) value of this second
  892.      point will be the same as that of the first point --
  893.      otherwise there is no way to distinguish among all of the
  894.      logical points at the center.  The center point will be labeled
  895.      Q instead of R as usual, and you will need to choose a third
  896.      point (S) to complete the rectangle PQRS.
  897.  
  898.    SEE ALSO: rez_drag, rez_ladj, rez_mesh, rez_zoom, rez_style
  899.  */
  900. {
  901.   done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  902.   undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  903.   lab1= Button(text="Adjust region.",x=.344,y=.895,dx=.050,dy=.012,width=-1);
  904.   zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  905.   sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);
  906.  
  907.   setp= Button(text="Set P",x=.208,y=.375,dx=.027,dy=.012);
  908.   setr= Button(text="Set R",x=.268,y=.375,dx=.027,dy=.012);
  909.   drag= Button(text="Click edge for EQ",x=.371,y=.375,
  910.            dx=.035,dy=.012,width=-1);
  911.   smth= Button(text="Smooth",x=.480,y=.375,dx=.035,dy=.012);
  912.   eqsp= Button(text="Smooth 4",x=.563,y=.375,dx=.041,dy=.012);
  913.  
  914.   mode= 1;
  915.   jP= jR= jQ= oldjP= oldjR= 0;
  916.   rez_plm, 1;
  917.   kmax= dimsof(rez_rt)(2);
  918.   lab1.text= "Select P.";
  919.   setp.text= "Set R";
  920.   done.text= "Cancel";
  921.   done.dx= 0.033;
  922.   button_plot, done, lab1, zorp, sets, setp;
  923.  
  924.   for (;;) {
  925.     m= mouse(1, 2, "");
  926.     if (is_void(m)) { rdline,prompt=""; continue; }
  927.     x= m(5);
  928.     y= m(6);
  929.     if (button_test(zorp,x,y)) {
  930.       rez_zoom;
  931.     } else if (button_test(sets,x,y)) {
  932.       rez_style;
  933.     } else if (button_test(setp,x,y)) {
  934.       mode= 1 + (setp.text!="Set P");
  935.       if (mode==1) {
  936.     lab1.text= "Select P.";
  937.     setp.text= "Set R";
  938.       } else {
  939.     lab1.text= "Select R.";
  940.     setp.text= "Set P";
  941.       }
  942.       jQ= 0;
  943.  
  944.     } else if (mode) {
  945.       if (button_test(done,x,y)) {
  946.     if (done.text=="Cancel") {
  947.       if (!oldjP || !oldjR) break;
  948.       jP= oldjP;
  949.       jR= oldjR;
  950.     } else {
  951.       k0= min(kP,kR);
  952.       k1= max(kP,kR);
  953.       l0= min(lP,lR);
  954.       l1= max(lP,lR);
  955.       zt= rez_zt(k0:k1,l0:l1);
  956.       rt= rez_rt(k0:k1,l0:l1);
  957.       list= array([zt,rt], 10);  /* keep last 10 for undo */
  958.     }
  959.     lab1.text= "Adjust region.";
  960.     setp.text= "Set P";
  961.     mode= jQ= 0;
  962.       } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
  963.          abs(_rez_yvp-y)<=_rez_dyvp) {
  964.     j= rez_find(m(1), m(2));
  965.     mold= mode;
  966.     if (numberof(_rez_center)>1) {
  967.       if (mode==2 && jP>0) {
  968.         kc= (_rez_center-1)%kmax + 1;
  969.         lc= (_rez_center-1)/kmax + 1;
  970.         if (noneof(kc-kc(1))) kc= kc(1);
  971.         else if (noneof(lc-lc(1))) lc= lc(1);
  972.         kd= (jP-1)%kmax + 1;
  973.         ld= (jP-1)/kmax + 1;
  974.         if (numberof(kc)==1 && kc!=kd && anyof(lc==ld))
  975.           j= kc + (ld-1)*kmax;
  976.         else if (numberof(lc)==1 && lc!=ld && anyof(kc==kd))
  977.           j= kd + (lc-1)*kmax;
  978.         lab1.text= "Select S.";
  979.         mode= 3;
  980.       } else {
  981.         j= 0;
  982.       }
  983.     }
  984.     if (j<1) {
  985.       rez_ding;
  986.       continue;
  987.     } else {
  988.       if (mode==1) jP= j;
  989.       else if (mode==2) jR= j;
  990.       else if (mode!=mold) jQ= j;
  991.       else if (kP==kQ) jR= (j-1)%kmax + 1 + (lQ-1)*kmax;
  992.       else jR= kQ + ((j-1)/kmax)*kmax;
  993.       kP= (jP-1)%kmax + 1;
  994.       lP= (jP-1)/kmax + 1;
  995.       kR= (jR-1)%kmax + 1;
  996.       lR= (jR-1)/kmax + 1;
  997.       kQ= (jQ-1)%kmax + 1;
  998.       lQ= (jQ-1)/kmax + 1;
  999.     }
  1000.       } else {
  1001.     rez_ding;
  1002.     continue;
  1003.       }
  1004.  
  1005.     } else {
  1006.       if (button_test(done,x,y)) break;
  1007.       if (button_test(undo,x,y)) {
  1008.     rez_zt(k0:k1,l0:l1)= list(,,1,1);
  1009.     rez_rt(k0:k1,l0:l1)= list(,,2,1);
  1010.     list(,,,1:-1)= list(,,,2:0);
  1011.       } else if (button_test(setr,x,y)) {
  1012.     oldjP= jP;
  1013.     oldjR= jR;
  1014.     lab1.text= "Select R.";
  1015.     mode= 2;
  1016.       } else if (button_test(smth,x,y)) {
  1017.     zt= rez_zt(k0:k1,l0:l1);
  1018.     if (dimsof(zt)(2)>2 && dimsof(zt)(3)>2) {
  1019.       rt= rez_rt(k0:k1,l0:l1);
  1020.       list(,,,2:0)= list(,,,1:-1);
  1021.       list(,,1,1)= zt;
  1022.       list(,,2,1)= rt;
  1023.       zt(2:-1,2:-1)= zt(zcen,zcen)(zcen,zcen);
  1024.       rt(2:-1,2:-1)= rt(zcen,zcen)(zcen,zcen);
  1025.       rez_zt(k0:k1,l0:l1)= zt;
  1026.       rez_rt(k0:k1,l0:l1)= rt;
  1027.     }
  1028.       } else if (button_test(eqsp,x,y)) {
  1029.     zt= rez_zt(k0:k1,l0:l1);
  1030.     if (dimsof(zt)(2)>2 && dimsof(zt)(3)>2) {
  1031.       rt= rez_rt(k0:k1,l0:l1);
  1032.       list(,,,2:0)= list(,,,1:-1);
  1033.       list(,,1,1)= zt;
  1034.       list(,,2,1)= rt;
  1035.       for (ii=0 ; ii<4 ; ii++) {
  1036.         zt(2:-1,2:-1)= zt(zcen,zcen)(zcen,zcen);
  1037.         rt(2:-1,2:-1)= rt(zcen,zcen)(zcen,zcen);
  1038.       }
  1039.       rez_zt(k0:k1,l0:l1)= zt;
  1040.       rez_rt(k0:k1,l0:l1)= rt;
  1041.     }
  1042.       } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
  1043.          abs(_rez_yvp-y)<=_rez_dyvp) {
  1044.     j= rez_find(m(1), m(2));
  1045.     kj= (j-1)%kmax + 1;
  1046.     lj= (j-1)/kmax + 1;
  1047.     inl= (lj>l0 && lj<l1);
  1048.     ink= (kj>k0 && kj<k1);
  1049.     zt= rez_zt(k0:k1,l0:l1);
  1050.     rt= rez_rt(k0:k1,l0:l1);
  1051.     if (j>0 && (kj==k0 || kj==k1) && inl) {
  1052.       list(,,,2:0)= list(,,,1:-1);
  1053.       list(,,1,1)= zt;
  1054.       list(,,2,1)= rt;
  1055.       ds= abs(zt(kj==k0,dif),rt(kj==k0,dif))(-,cum);
  1056.       ds/= ds(1,0);
  1057.       rez_zt(k0:k1,l0:l1)= zt(,1) + (zt(,0)-zt(,1))*ds
  1058.       rez_rt(k0:k1,l0:l1)= rt(,1) + (rt(,0)-rt(,1))*ds
  1059.     } else if (j>0 && (lj==l0 || lj==l1) && ink) {
  1060.       list(,,,2:0)= list(,,,1:-1);
  1061.       list(,,1,1)= zt;
  1062.       list(,,2,1)= rt;
  1063.       ds= abs(zt(dif,lj==l0),rt(dif,lj==l0))(cum);
  1064.       ds/= ds(0);
  1065.       rez_zt(k0:k1,l0:l1)= zt(1,-,) + (zt(0,-,)-zt(1,-,))*ds
  1066.       rez_rt(k0:k1,l0:l1)= rt(1,-,) + (rt(0,-,)-rt(1,-,))*ds
  1067.     } else {
  1068.       rez_ding;
  1069.       continue;
  1070.     }
  1071.       } else {
  1072.     rez_ding;
  1073.     continue;
  1074.       }
  1075.     }
  1076.     rez_plm, !mode, 1;
  1077.     if (!mode || (jP && jR && kR!=kP && lR!=lP)) {
  1078.       done.text= "OK";
  1079.       done.dx= 0.020;
  1080.     } else {
  1081.       done.text= "Cancel";
  1082.       done.dx= 0.033;
  1083.     }
  1084.     button_plot, done, lab1, zorp, sets, setp;
  1085.     if (jP) plg, rez_rt(kP,lP), rez_zt(kP,lP),
  1086.       marker='P', type=0, legend=string(0);
  1087.     if (jR) plg, rez_rt(kR,lR), rez_zt(kR,lR),
  1088.       marker='R', type=0, legend=string(0);
  1089.     if (jQ) plg, rez_rt(kQ,lQ), rez_zt(kQ,lQ),
  1090.       marker='Q', type=0, legend=string(0);
  1091.     if (done.text=="OK") {
  1092.       if (!jQ) plg, rez_rt(kP,lR), rez_zt(kP,lR),
  1093.                  marker='Q', type=0, legend=string(0);
  1094.       plg, rez_rt(kR,lP), rez_zt(kR,lP),
  1095.         marker='S', type=0, legend=string(0);
  1096.     }
  1097.     if (!mode) button_plot, undo, setr, drag, smth, eqsp;
  1098.     else if (jP || jR) {
  1099.       text= jP? swrite(format="P=(%ld,%ld)",kP,lP) : "";
  1100.       if (jR) text+= swrite(format="    R=(%ld,%ld)",kR,lR);
  1101.       else if (jQ) text+= swrite(format="    Q=(%ld,%ld)",kQ,lQ);
  1102.       plt, text, 0.35, 0.375;
  1103.     }
  1104.   }
  1105.  
  1106.   rez_plm, 1;
  1107. }
  1108.  
  1109. _rez_zoom= 0n;
  1110.  
  1111. func rez_zoom
  1112. /* DOCUMENT rez_zoom
  1113.      Change limits after setting rez_mesh.
  1114.  
  1115.      There are two modes of operation:
  1116.      (1) P-mode:
  1117.          Click on a point to magnify or demagnify about that point.
  1118.      Drag the point to move it to a different position in the
  1119.      viewport.  The left button zooms in (magnifies by 1.5), the
  1120.      right button zooms out (demagnifies by 1.5), and the middle
  1121.      button pans (no magnification change).  If you click just
  1122.      below or just above the tick marks, only the x coordinate
  1123.      is changed; clicking just to the left or just to the right
  1124.      of the viewport changes only the y coordinate.
  1125.      (2) R-mode:
  1126.          Left drag out a rectangle to set the limits to that rectangle.
  1127.      Right drag out a rectangle to cram the entire current viewport
  1128.      into that rectangle (an analog to zooming out).
  1129.      Middle drag to pan as in P-mode (the rubber band rectangle is
  1130.      meaningless in this case).
  1131.  
  1132.       Click the [R-mode] (or [P-mode]) button to change modes.
  1133.  
  1134.       Click the [OK] button when you are satisfied with the limits.
  1135.  
  1136.       Click the [Undo] button to remove the effect of the previous
  1137.       zoom-click.  Up to 100 successive undo operations are allowed.
  1138.  
  1139.       Click the [Polar] button to toggle polar coordinates.  The
  1140.       vertical axis will be theta, and the horizontal axis radius.
  1141.       The polar coordinate origin will be at (zt,rt) = (0,0).
  1142.  
  1143.       Click the [Next], [Prev], or [First] buttons to focus in on
  1144.       zones which are bowtied or chevroned.  These zones are marked
  1145.       by a B or a C, respectively (if rez_mark is set).
  1146.  
  1147.    SEE ALSO: rez_mesh
  1148.  */
  1149. {
  1150.   done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  1151.   undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  1152.   lab2= Button(text="Left=in  Middle=pan  Right=out",
  1153.            x=.413,y=.895,dx=.128,dy=.012,width=-1);
  1154.   mode= Button(text=(_rez_zoom?"P-Mode":"R-Mode"),
  1155.            x=.584,y=.895,dx=.037,dy=.012);
  1156.  
  1157.   full= Button(text="Full Mesh",x=.230,y=.375,dx=.046,dy=.012);
  1158.   polar= Button(text="Polar",x=.307,y=.375,dx=.025,dy=.012);
  1159.   lab3= Button(text="Go To Bowtie:",
  1160.            x=.402,y=.375,dx=.040,dy=.012,width=-1);
  1161.   next= Button(text="Next",x=.486,y=.375,dx=.025,dy=.012);
  1162.   prev= Button(text="Prev",x=.536,y=.375,dx=.025,dy=.012);
  1163.   first= Button(text="First",x=.587,y=.375,dx=.026,dy=.012);
  1164.  
  1165.   rez_plm, 1;
  1166.   button_plot, done, undo, lab2, mode, full, polar, lab3, next, prev, first;
  1167.  
  1168.   list= array(0.0, 5, 100);  /* keep last 100 limits for undo */
  1169.   len= 0;
  1170.   for (;;) {
  1171.     m= mouse(1, _rez_zoom, "");
  1172.     if (is_void(m)) { rdline,prompt=""; continue; }
  1173.     x= m(5);
  1174.     y= m(6);
  1175.     if (button_test(done,x,y)) break;
  1176.     if (button_test(undo,x,y)) {
  1177.       if (len) {
  1178.     limits, list(,1);
  1179.     list(,1:-1)= list(,2:0);
  1180.     len--;
  1181.       } else {
  1182.     rez_ding;
  1183.     continue;
  1184.       }
  1185.     } else if (button_test(mode,x,y)) {
  1186.       _rez_zoom= !_rez_zoom;
  1187.       mode.text= (_rez_zoom?"P-Mode":"R-Mode");
  1188.     } else if (button_test(full,x,y)) {
  1189.       limits;  redraw;
  1190.       continue;
  1191.     } else if (button_test(polar,x,y)) {
  1192.       rez_toggle;
  1193.       limits;
  1194.     } else if (button_test(next,x,y)) {
  1195.       rez_next;  redraw;
  1196.       continue;
  1197.     } else if (button_test(prev,x,y)) {
  1198.       rez_prev;  redraw;
  1199.       continue;
  1200.     } else if (button_test(first,x,y)) {
  1201.       rez_next, 1;  redraw;
  1202.       continue;
  1203.     } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
  1204.            abs(_rez_yvp-y)<=_rez_dyvp) {
  1205.       x= m(1);
  1206.       y= m(2);
  1207.       x1= m(3);
  1208.       y1= m(4);
  1209.       button= long(m(10));
  1210.       lims= limits();
  1211.       if (!_rez_zoom || button==2) {  /* point mode */
  1212.     if (button==1) f= 1.5;
  1213.     else if (button==2) f= 1.0;
  1214.     else f= 1.0/1.5;
  1215.     x0= x - (x1-lims(1))/f;
  1216.     x1= x - (x1-lims(2))/f;
  1217.     y0= y - (y1-lims(3))/f;
  1218.     y1= y - (y1-lims(4))/f;
  1219.     if (x!=lims(1) && x!=lims(2)) {
  1220.       if (y!=lims(3) && y!=lims(4)) limits, x0,x1,y0,y1;
  1221.       else limits, x0,x1;
  1222.     } else {
  1223.       if (y!=lims(3) && y!=lims(4)) limits, ,,y0,y1;
  1224.       else limits, x0,x1,y0,y1;
  1225.     }
  1226.     list(,2:0)= list(,1:-1);
  1227.     list(,1)= lims;
  1228.     len++;
  1229.       } else if (x!=x1 && y!=y1) {  /* rectangle mode */
  1230.     x0= min(x,x1);
  1231.     x1= max(x,x1);
  1232.     y0= min(y,y1);
  1233.     y1= max(y,y1);
  1234.     if (button!=1) {
  1235.       f= (lims(2)-lims(1))/(x1-x0);
  1236.       x0= lims(1) - f*(x0-lims(1));
  1237.       x1= lims(2) + f*(lims(2)-x1);
  1238.       f= (lims(4)-lims(3))/(y1-y0);
  1239.       y0= lims(3) - f*(y0-lims(3));
  1240.       y1= lims(4) + f*(lims(4)-y1);
  1241.     }
  1242.     limits, x0,x1,y0,y1;
  1243.     list(,2:0)= list(,1:-1);
  1244.     list(,1)= lims;
  1245.     len++;
  1246.       } else {
  1247.     rez_ding;
  1248.     continue;
  1249.       }
  1250.     }
  1251.     rez_plm, 0;
  1252.     button_plot, done, undo, lab2, mode, full, polar, lab3, next, prev, first;
  1253.   }
  1254. }
  1255.  
  1256. func rez_style
  1257. {
  1258.   done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  1259.   klin= Button(text="K-line",x=.290,y=.895,dx=.038,dy=.012,width=6.);
  1260.   llin= Button(text="L-line",x=.366,y=.895,dx=.038,dy=.012);
  1261.   type= Button(text="Type",x=.476,y=.895,dx=.025,dy=.012,width=6.);
  1262.   wdth= Button(text="Width",x=.530,y=.895,dx=.029,dy=.012);
  1263.   colr= Button(text="Color",x=.586,y=.895,dx=.027,dy=.012);
  1264.  
  1265.   bar1= ["1", "2", "3", "4", "5", "6", "7", "8"];
  1266.   bar2= ["bg", "fg", "blk", "wht",
  1267.      "red", "grn", "blu", "cyn", "mag", "yel"];
  1268.   bar= array(Button, 10);
  1269.   bar.dx= bardx= .022;
  1270.   bar.x= barx= .644;
  1271.   bar.dy= bardy= .012;
  1272.   bar.y= .895-(indgen(10)-1)*2.*bardy;
  1273.   bary= avg(bar.y);
  1274.  
  1275.   bar(1:5).text= bar1(1:5);
  1276.   barn= 5;
  1277.   bar(rez_kstyle(1)).width= 6.;
  1278.   change= 1;
  1279.  
  1280.   for (;;) {
  1281.     if (change) {
  1282.       i= where(bar.width)(1);
  1283.       if (type.width) {
  1284.     if (klin.width) rez_kstyle(1)= i;
  1285.     else rez_lstyle(1)= i;
  1286.       } else if (wdth.width) {
  1287.     if (klin.width) rez_kstyle(2)= i;
  1288.     else rez_lstyle(2)= i;
  1289.       } else if (colr.width) {
  1290.     if (klin.width) rez_kstyle(3)= -i;
  1291.     else rez_lstyle(3)= -i;
  1292.       }
  1293.       rez_plm, 0;
  1294.       button_plot, done, klin, llin, type, wdth, colr, bar(1:barn);
  1295.       change= 0;
  1296.     }
  1297.     m= mouse(0, _rez_zoom, "");
  1298.     if (is_void(m)) { rdline,prompt=""; continue; }
  1299.     x= m(5);
  1300.     y= m(6);
  1301.     if (button_test(done,x,y)) break;
  1302.     if (button_test(klin,x,y)) {
  1303.       if (klin.width) continue;
  1304.       klin.width= 6.;
  1305.       llin.width= 0.;
  1306.       bar.width= 0.;
  1307.       if (type.width) bar(rez_kstyle(1)).width= 6.;
  1308.       else if (wdth.width) bar(rez_kstyle(2)).width= 6.;
  1309.       else if (colr.width) bar(-rez_kstyle(3)).width= 6.;
  1310.     } else if (button_test(llin,x,y)) {
  1311.       if (llin.width) continue;
  1312.       llin.width= 6.;
  1313.       klin.width= 0.;
  1314.       bar.width= 0.;
  1315.       if (type.width) bar(rez_lstyle(1)).width= 6.;
  1316.       else if (wdth.width) bar(rez_lstyle(2)).width= 6.;
  1317.       else if (colr.width) bar(-rez_lstyle(3)).width= 6.;
  1318.     } else if (button_test(type,x,y)) {
  1319.       if (type.width) continue;
  1320.       type.width= 6.;
  1321.       wdth.width= colr.width= 0.;
  1322.       bar(1:5).text= bar1(1:5);
  1323.       barn= 5;
  1324.       bar.width= 0.;
  1325.       if (klin.width) bar(rez_kstyle(1)).width= 6.;
  1326.       else bar(rez_lstyle(1)).width= 6.;
  1327.     } else if (button_test(wdth,x,y)) {
  1328.       if (wdth.width) continue;
  1329.       wdth.width= 6.;
  1330.       type.width= colr.width= 0.;
  1331.       bar(1:8).text= bar1;
  1332.       barn= 8;
  1333.       bar.width= 0.;
  1334.       if (klin.width) bar(rez_kstyle(2)).width= 6.;
  1335.       else bar(rez_lstyle(2)).width= 6.;
  1336.     } else if (button_test(colr,x,y)) {
  1337.       if (colr.width) continue;
  1338.       colr.width= 6.;
  1339.       type.width= wdth.width= 0.;
  1340.       bar.text= bar2;
  1341.       barn= 10;
  1342.       bar.width= 0.;
  1343.       if (klin.width) bar(-rez_kstyle(3)).width= 6.;
  1344.       else bar(-rez_lstyle(3)).width= 6.;
  1345.     } else if (abs(barx-x)<bardx && abs(bary-y)<2.*barn*bardy) {
  1346.       i= long((bar(1).y+bardy - y)/(2.0*bardy)) + 1;
  1347.       if (i>0 && i<=barn) {
  1348.     bar.width= 0.;
  1349.     bar(i).width= 6.;
  1350.       } else {
  1351.     rez_ding;
  1352.     continue;
  1353.       }
  1354.     } else {
  1355.       rez_ding;
  1356.       continue;
  1357.     }
  1358.     change= 1;
  1359.   }
  1360. }
  1361.  
  1362. extern rez_qrt, rez_qzt, rez_qireg;
  1363. /* DOCUMENT rez_qrt, rez_qzt, rez_qireg
  1364.      are the names of the mesh variables for rez_mesh, "rt", "zt", and
  1365.      "ireg" by default.  Note that ireg is optional.
  1366.  */
  1367. rez_qzt= "zt";
  1368. rez_qrt= "rt";
  1369. rez_qireg= "ireg";
  1370.  
  1371. func rez_mesh(rt, zt, ireg, all=, quiet=)
  1372. /* DOCUMENT rez_mesh, rt, zt, ireg
  1373.        -or- rez_mesh, rt, zt
  1374.        -or- rez_mesh, zfile
  1375.      set mesh for rez_... commands.  If ZFILE is specified, it must be a
  1376.      binary file containing variables RT, ZT, and (optionally) IREG.
  1377.    SEE ALSO: rez_all, rez_adjust_mesh
  1378.  */
  1379. {
  1380.   extern rez_rt0, rez_zt0, rez_ireg0, rez_rt, rez_zt, rez_ireg;
  1381.   extern rez_mapi;
  1382.  
  1383.   if (is_stream(rt)) {
  1384.     f= rt;
  1385.     vars= get_vars(f);
  1386.     ireg= rez_getvar(f,vars, rez_qireg);
  1387.     zt= rez_getvar(f,vars, rez_qzt);
  1388.     rt= rez_getvar(f,vars, rez_qrt);
  1389.   }
  1390.   if (is_void(ireg)) {
  1391.     ireg= array(1, dimsof(rt));
  1392.     ireg(1,)= 0;
  1393.     ireg(,1)= 0;
  1394.   }
  1395.  
  1396.   rez_rt0= rez_rt= rt;
  1397.   rez_zt0= rez_zt= zt;
  1398.   rez_ireg0= rez_ireg= ireg;
  1399.   if (is_void(zt) || is_void(rt)) return;
  1400.   rez_ptexist;
  1401.  
  1402.   rez_mapi= 0;
  1403.   if (!quiet) nbow, rez_rt, rez_zt, rez_ireg, all=all;
  1404.  
  1405.   rez_plm, 1;
  1406. }
  1407.  
  1408. func rez_next(which)
  1409. /* DOCUMENT rez_next
  1410.      zoom in on next bowtied (or chevroned) zone (after rez_mesh).
  1411.      With numeric argument, zooms to that bowtie (or chevron).  With
  1412.      numeric argument 0, zooms to current bowtie (or chevron).
  1413.    SEE ALSO: rez_prev, rez_mark, rez_drag
  1414.  */
  1415. {
  1416.   list= where(rez_map==0);
  1417.   n= where(rez_map==1);
  1418.   if (!numberof(list)) list= [];
  1419.   if (!numberof(n)) n= [];
  1420.   list= grow(list,n);
  1421.   n= numberof(list);
  1422.   if (is_void(which)) which= rez_mapi+1;
  1423.   else if (!which) which= rez_mapi? rez_mapi : 1;
  1424.   else if (which<0) which= rez_mapi>1? rez_mapi-1 : n+1;
  1425.   if (!n || which>n) { rez_ding; return; }
  1426.   local rr, zz;
  1427.   rez_getrz, list(which);
  1428.   rn= min(rr);  rx= max(rr);
  1429.   zn= min(zz);  zx= max(zz);
  1430.   rr= rx-rn;
  1431.   zz= zx-zn;
  1432.   zn-= 0.3*zz;
  1433.   zx+= 0.3*zz;
  1434.   rn-= 0.3*rr;
  1435.   rx+= 0.3*rr;
  1436.   limits, zn, zx, rn, rx;
  1437.   rez_mapi= which;
  1438. }
  1439.  
  1440. func rez_prev
  1441. /* DOCUMENT rez_prev
  1442.      zoom in on previous bowtied (or chevroned) zone (after rez_mesh).
  1443.    SEE ALSO: rez_next, rez_mark, rez_drag
  1444.  */
  1445. {
  1446.   rez_next, -1;
  1447. }
  1448.  
  1449. func rez_getrz(zlist)
  1450. {
  1451.   extern rr, zz;   /* 2-by-2-by-dimsof(zlist) result */
  1452.   if (!numberof(zlist)) {
  1453.     rr= zz= [];
  1454.     return;
  1455.   }
  1456.   dims= dimsof(rez_rt);
  1457.   kmax= dims(2);
  1458.   klist= (zlist-1)%(kmax-1);
  1459.   llist= (zlist-1)/(kmax-1);
  1460.   list= llist*kmax + klist + 1;  /* lower left corner index */
  1461.   rr= zz= array(0.0, 2, 2, dimsof(list));
  1462.   rr(1,1,..)= rez_rt(list);
  1463.   zz(1,1,..)= rez_zt(list);
  1464.   rr(2,1,..)= rez_rt(list+1);
  1465.   zz(2,1,..)= rez_zt(list+1);
  1466.   rr(1,2,..)= rez_rt(list+kmax);
  1467.   zz(1,2,..)= rez_zt(list+kmax);
  1468.   rr(2,2,..)= rez_rt(list+kmax+1);
  1469.   zz(2,2,..)= rez_zt(list+kmax+1);
  1470. }
  1471.  
  1472. func rez_ptexist
  1473. {
  1474.   extern rez_ptx, rez_ptlist, rez_ireg;
  1475.   rez_ireg(1,)= 0;
  1476.   rez_ireg(,1)= 0;
  1477.   d= dimsof(rez_ireg);
  1478.   ix= d(2);
  1479.   jx= d(3);
  1480.   n= numberof(rez_ireg);
  1481.   ireg= array(0, n+ix+1);
  1482.   ireg(1:n)= rez_ireg(*);
  1483.   rez_ptx= array(0, dimsof(rez_ireg));
  1484.   rez_ptx(*)=
  1485.     (ireg(1:n) | ireg(2:n+1) | ireg(ix+1:n+ix) | ireg(ix+2:n+ix+1));
  1486.   rez_ptlist= where(rez_ptx);
  1487. }
  1488.  
  1489. extern _rez_center;
  1490.  
  1491. func rez_find(x, y)
  1492. {
  1493.   extern _rez_center;
  1494.   d= abs(rez_zt-x, rez_rt-y)(rez_ptlist);
  1495.   j= d(mnx);
  1496.   d0= d(j);
  1497.   j0= rez_ptlist(j);
  1498.   j= where(!(d-d0));  /* detect center points */
  1499.   if (numberof(j)>1) _rez_center= rez_ptlist(j);
  1500.   else _rez_center= [];
  1501.   return j0;
  1502. }
  1503.  
  1504. func rez_getvar(f, vars, name)
  1505. {
  1506.   v= vars(2);
  1507.   if (v && anyof(*v == name)) return get_member(f,name);
  1508.   v= vars(1);
  1509.   if (v && anyof(*v == name)) return get_member(f,name);
  1510.   return [];
  1511. }
  1512.  
  1513. func rez_range(x) { return x; }  /* parser quirk */
  1514.  
  1515. func rez_ding
  1516. {
  1517.   write, format="%s", "\a\a";
  1518. }
  1519.  
  1520. func rez_solve(ss, nn)
  1521. /* xxDOCUMENT adapted from gseries_r(s, n) in series.i
  1522.      returns the ratio r of the finite geometric series, given the sum s:
  1523.         1 + r + r^2 + r^3 + ... + r^(nn-1) = ss,
  1524.     or (r^nn-1)/(r-1) = ss
  1525.  
  1526.      Altered to solve only nn>0.0, but allows any real nn.
  1527.  */
  1528. {
  1529.   /* compute an approximate result which has exact values and
  1530.      derivatives at s==1, s==n, and s->infinity --
  1531.      different approximations apply for s>n and s<n */
  1532.   if (nn==2) return ss-1.0;
  1533.   if (ss>nn) {
  1534.     pow= 1.0/(nn-1.0);
  1535.     npow= nn^pow - 1.0;
  1536.     n2r= 1.0/(nn-2.0);
  1537.     A= (2.0-nn*npow)*n2r;
  1538.     B= (2.0*npow-nn*pow)*nn*n2r;
  1539.     r= ss^pow - pow + A*(nn/ss)^pow + B/ss;
  1540.   } else {
  1541.     sn= (ss-1.0)/(nn-1.0);
  1542.     n2r= 1.0/(nn*nn);
  1543.     r= 1.0 - 1.0/ss + n2r*sn*sn*(nn+1.0 - sn);
  1544.   }
  1545.  
  1546.   /* Polish the approximation using Newton-Raphson iterations.  */
  1547.   if (ss!=nn) {
  1548.     for (;;) {
  1549.       rr= r-1.0;
  1550.       rn= r^(nn-1);
  1551.       rrss= rr*ss;
  1552.       delta= rrss - (r*rn-1.0);
  1553.       if (abs(delta)<=1.e-9*abs(rrss)) break;
  1554.       r+= delta/(nn*rn-ss);
  1555.     }
  1556.     /* try to get it to machine precision */
  1557.     if (delta) r+= delta/(nn*rn-ss);
  1558.   }
  1559.  
  1560.   return r;
  1561. }
  1562.  
  1563. func rez_test
  1564. {
  1565.   extern rez_rt, rez_zt;
  1566.   x= span(0,1,10)(,-:1:7);  y= span(0,.5,7)(-:1:10,);
  1567.   rez_mesh, x+y, x-y+.5;
  1568. }
  1569.  
  1570. if (!is_void(rez_load_hook)) include, rez_load_hook;
  1571.